<?php
/* -=-=-=-=-=-=-=-=-=-=-=-=-
     author by terry
-=-=-=-=-=-=-=-=-=-=-=-=- */
error_reporting(7);
require "global.php";

$recruiters = $DB->fetch_all("SELECT * FROM pa_recuiter");
foreach($recruiters as $rec){
	$recOptions[$rec['rc_id']] =  $rec['rc_uname']."(".$rec['rc_fname'] ." ".$rec['rc_lname'].")";
}

cpheader();
//set offset
if(!empty($_POST[offset]))
{$offset = $_POST[offset];}
else if  (!empty($_GET[offset]))
{$offset = $_GET[offset]; }
else  {$offset = 0; }


if ($_GET[action]=="add"||$_GET[action]=='mod'||$_GET[action]=='view'){

    $cpforms->inithtmlarea();
	if($_GET[action]=="add"){
			$cpforms->formheader(array('title'=>'Add New Contact (<font color=red>*</font>=Required Field)',
										'name'=>'article'));

			$cpforms->makehidden(array('name'=>'action',
									   'value'=>'doinsert'));
	}
	if($_GET[action] == "mod"){
			$cpforms->formheader(array('title'=>'Modify Contact (<font color=red>*</font>=Required Field)',
										'name'=>'article'));

			$cpforms->makehidden(array('name'=>'action',
									   'value'=>'update'));
		    $contactId = intval($_GET[id]);
			$cpforms->makehidden(array('name'=>'id',
									   'value'=>$contactId));
			$cpforms->makehidden(array('name'=>'offset',
									   'value'=>$offset));
            $read =1;
	        $rs  = $DB->fetch_one_array("SELECT * FROM pa_mycontacts WHERE myc_id =".$contactId);
	}

	if($_GET[action]=="view"){
				$cpforms->formheader(array('title'=>'View Contact (<font color=red>*</font>=Required Field)',
										'name'=>'article',
				                        'enctype'=>'multipart/form-data'));
		    $contactId = intval($_GET[id]);
			$cpforms->makehidden(array('name'=>'id',
									   'value'=>$contactId));
			$cpforms->makehidden(array('name'=>'offset',
									   'value'=>$offset));
            $view =1;
	        $rs  = $DB->fetch_one_array("SELECT * FROM pa_mycontacts WHERE myc_id =".$contactId);
	}
	?>
     <?       

		$cpforms->gethightext(array('text'=>"First Name",'name'=>"myc_firstname",'only'=>1,'value'=>"$rs[myc_firstname]",'view'=>"$view"));
		$cpforms->gethightext(array('text'=>"Last Name",'name'=>"myc_lastname",'only'=>1,'value'=>"$rs[myc_lastname]",'view'=>"$view"));
		$cpforms->gethightext(array('text'=>"Email",'name'=>"myc_email",'only'=>1,'value'=>"$rs[myc_email]",'view'=>"$view"));
		$cpforms->makeselect(array('text'=>"Recruiter",'name'=>"myc_rcid",'selected'=>"$rs[myc_rcid]",'view'=>"$view",'option'=> $recOptions));

		$cpforms->gethightext(array('text'=>"Job Title",'name'=>"myc_jobtitle",'value'=>"$rs[myc_jobtitle]",'view'=>"$view"));
		$cpforms->gethightext(array('text'=>"Company",'name'=>"myc_company",'value'=>"$rs[myc_company]",'view'=>"$view"));
		$cpforms->gethightext(array('text'=>"Address 1",'name'=>"myc_address1",'value'=>"$rs[myc_address1]",'view'=>"$view"));
		$cpforms->gethightext(array('text'=>"Address 2",'name'=>"myc_address2",'value'=>"$rs[myc_address2]",'view'=>"$view"));
		$cpforms->gethightext(array('text'=>"City",'name'=>"myc_city",'value'=>"$rs[myc_city]",'view'=>"$view"));
		$cpforms->makeselect(array('one'=>1,'text'=>"State",'name'=>"myc_state",'selected'=>"$rs[myc_state]",'view'=>"$view",'option'=> $state));
		$cpforms->gethightext(array('text'=>"Zip Code",'name'=>"myc_zipcode",'value'=>"$rs[myc_zipcode]",'view'=>"$view"));
		$cpforms->makeselect(array('one'=>1,'text'=>"Country",'name'=>"myc_country",'selected'=>"$rs[myc_country]",'view'=>"$view",'option'=> $country));
		$cpforms->gethightext(array('text'=>"Mobile Phone",'name'=>"myc_mobilephone",'value'=>"$rs[myc_mobilephone]",'view'=>"$view"));
		$cpforms->gethightext(array('text'=>"Phone",'name'=>"myc_phone",'value'=>"$rs[myc_phone]",'view'=>"$view"));
		$cpforms->gethightext(array('text'=>"Fax",'name'=>"myc_fax",'value'=>"$rs[myc_fax]",'view'=>"$view"));
		$cpforms->gethightext(array('text'=>"Website",'name'=>"myc_website",'value'=>"$rs[myc_website]",'view'=>"$view"));
		$cpforms->makeselect(array('text'=>"Industry",'name'=>"myc_industry",'selected'=>"$rs[myc_industry]",'view'=>"$view",'option'=> $jobcategory));
		$cpforms->makeselect(array('text'=>"Speciality 1",'name'=>"myc_speciality1",'selected'=>"$rs[myc_speciality1]",'view'=>"$view",'option'=> $jobfunction));
		$cpforms->makeselect(array('text'=>"Speciality 2",'name'=>"myc_speciality2",'selected'=>"$rs[myc_speciality2]",'view'=>"$view",'option'=> $jobfunction));
		$cpforms->makeselect(array('text'=>"Speciality 3",'name'=>"myc_speciality3",'selected'=>"$rs[myc_speciality3]",'view'=>"$view",'option'=> $jobfunction));
		$cpforms->maketextarea(array('text'=>"Notes",'name'=>"myc_notes",'id'=>"myc_notes",'value'=>"$rs[myc_notes]",'view'=>"$view"));
	
	 ?>

<?

   if($_GET[action]=="view"){$cpforms->noformfooter();}
	   else{ $cpforms->formfooter(array('confirm'=>1));}
}


if ($_GET[action]=="list"){


	if($_GET[action]=="list"){   
		$nav = new buildNav;

		$total = $DB->fetch_one_array("SELECT COUNT(*) AS count FROM pa_mycontacts");

		$nav->total_result = $total[count];

		if (empty($total[count])) {
			pa_exit("<font color=red>No contact!</font><input type=button value='Add New' onclick='location.href=\"mycontact.php?action=add\"'>");
		}

		$nav->execute("SELECT * FROM pa_mycontacts ORDER BY myc_id DESC");

    }
	
	echo $nav->title("Recuriter's Contact list");
    echo $nav->pagenav();

    echo "<table border=\"0\" width=\"100%\" cellpadding=\"4\" cellspacing=\"1\" class=\"tableoutline\">
	           <tr><td colspan=7><input type=button value=\"Add New Contact\" onclick=\"location.href='mycontact.php?action=add'\"></td></tr>
               <tr align=\"center\" class=\"tbhead\">
                <td nowrap  width=\"6%\">Contact Name</td>
                <td nowrap  width=\"10%\">Recruiter</td>
                <td nowrap  width=\"10%\">Email</td>
                <td nowrap  width=\"10%\">Jobtitle</td>
				 <td nowrap  width=\"18%\"> Phone </td>
				<td nowrap  width=\"6%\">Industry</td>
                <td nowrap  width=\"10%\">City </td>
                <td nowrap  width=\"10%\">State</td>
				 <td nowrap  width=\"22%\">Country</td>
				 <td nowrap  width=\"10%\"> Action </td>
               </tr>\n";
    while ($list = $DB->fetch_array($nav->sql_result)){
           echo "<tr class=".getrowbg().">
                      <td align=\"left\" nowrap>".$list[myc_firstname]." ".$list[myc_lastname]."</td>
                      <td align=\"left\">".$recOptions[$list[myc_rcid]]."</td>
                      <td align=\"left\">".$list[myc_email]."</td>
					  <td align=\"left\">".$list[myc_jobtitle]."</td>
					  <td align=\"left\">".$list[myc_phone]."</td>
					  <td align=\"left\">".$jobcategory[$list[myc_industry]]."</td>
					  <td align=\"left\">".$list[myc_city]."</td>
					  <td align=\"left\">".$state[$list[myc_state]]."</td>
					  <td align=\"left\">".$country[$list[myc_country]]."</td>
                      <td align=\"left\" nowrap><a href=\"mycontact.php?action=view&offset=$_GET[offset]&id=$list[myc_id]\">View</a> | <a href=\"mycontact.php?action=mod&offset=$_GET[offset]&id=$list[myc_id]\">Edit</a> | <a href=\"mycontact.php?action=kill&offset=$_GET[offset]&id=$list[myc_id]\">Delete</a>";
    }
	if(empty($total[count])){
			echo "<tr><td><font color=red>No results!</font></td></tr>";
   }
    echo "</table>\n";
    echo $nav->pagenav();
}

if ($_GET[action]=="kill"){
    $cpforms->formheader(array('title'=>'Sure to delete this?'));
    $cpforms->makehidden(array('name'=>'action',
                                'value'=>'remove'));
    $cpforms->makehidden(array('name'=>'contactId',
                                'value'=>$_GET[id]));
	$cpforms->makehidden(array('name'=>'offset',
                                'value'=>$_GET[offset]));
    $cpforms->formfooter(array('confirm'=>2));
}


if( $_POST[action]=="remove"){
    $DB->query("DELETE FROM pa_mycontacts WHERE myc_id='$_POST[contactId]'");
    redirect("./mycontact.php?action=list&offset=".$offset,"<font color=red>Deleted Successfully!</font>");
}



if ($_POST[action]=="doinsert"){
    
	$myc_rcid      = $_POST[myc_rcid];
	$myc_firstname		= htmlspecialchars(trim($_POST[myc_firstname]));
	$myc_lastname		= htmlspecialchars(trim($_POST[myc_lastname]));
	$myc_email			= htmlspecialchars(trim($_POST[myc_email]));
	$myc_jobtitle		= htmlspecialchars(trim($_POST[myc_jobtitle]));
	$myc_company		= htmlspecialchars(trim($_POST[myc_company]));
	$myc_address1       = htmlspecialchars(trim($_POST[myc_address1]));
	$myc_address2      = htmlspecialchars(trim($_POST[myc_address2]));
	$myc_city     = htmlspecialchars(trim($_POST[myc_city]));
	$myc_state   = htmlspecialchars(trim($_POST[myc_state]));
	$myc_zipcode   = htmlspecialchars(trim($_POST[myc_zipcode]));
	$myc_country   = htmlspecialchars(trim($_POST[myc_country]));
	$myc_mobilephone      = htmlspecialchars(trim($_POST[myc_mobilephone]));
	$myc_phone     = htmlspecialchars(trim($_POST[myc_phone]));
	$myc_fax  = htmlspecialchars(trim($_POST[myc_fax]));
	$myc_website   = htmlspecialchars(trim($_POST[myc_website]));
	$myc_industry   = htmlspecialchars(trim($_POST[myc_industry]));
	$myc_speciality1   = htmlspecialchars(trim($_POST[myc_speciality1]));
	$myc_speciality2    = htmlspecialchars(trim($_POST[myc_speciality2]));
	$myc_speciality3       = htmlspecialchars(trim($_POST[myc_speciality3]));
	$myc_notes      = htmlspecialchars(trim($_POST[myc_notes]));

	$sql = "INSERT INTO `pa_mycontacts` ( 
			`myc_rcid` , 
			`myc_firstname` ,
			`myc_lastname` , 
			`myc_email` , 
			`myc_jobtitle` ,
			`myc_company`,
			`myc_address1`,
			`myc_address2` , 
			`myc_city` ,
			`myc_state` ,
			`myc_zipcode`,
			`myc_country` ,
			`myc_mobilephone` ,
			`myc_phone` ,
			`myc_fax` ,
			`myc_website`, 
			`myc_industry` ,
			`myc_speciality1`,
			`myc_speciality2` ,
			`myc_speciality3` ,
			`myc_notes` ,
			`myc_created` 
			) VALUES (
			'".$myc_rcid."', 
			'".$myc_firstname."', 
			'".$myc_lastname."',
			'".$myc_email."',
			'".$myc_jobtitle."',
			'".$myc_company."',
			'".$myc_address1."', 
			'".$myc_address2."',
			'".$myc_city."',
			'".$myc_state."',
			'".$myc_zipcode."', 
			'".$myc_country."',
			'".$myc_mobilephone."', 
			'".$myc_phone."',
			'".$myc_fax."',
			'".$myc_website."', 
			'".$myc_industry."',
			'".$myc_speciality1."',
			'".$myc_speciality2."', 
			'".$myc_speciality3."', 
			'".$myc_notes."', '".time()."');";

	$DB->query($sql);
    redirect("./mycontact.php?action=list","<font color=red>Added Successfully!</font>");
}


if ($_POST[action]=="update"){

	$myc_id		 = $_POST[id];
	$myc_rcid      = $_POST[myc_rcid];
	$myc_firstname		= htmlspecialchars(trim($_POST[myc_firstname]));
	$myc_lastname		= htmlspecialchars(trim($_POST[myc_lastname]));
	$myc_email			= htmlspecialchars(trim($_POST[myc_email]));
	$myc_jobtitle		= htmlspecialchars(trim($_POST[myc_jobtitle]));
	$myc_company		= htmlspecialchars(trim($_POST[myc_company]));
	$myc_address1       = htmlspecialchars(trim($_POST[myc_address1]));
	$myc_address2      = htmlspecialchars(trim($_POST[myc_address2]));
	$myc_city     = htmlspecialchars(trim($_POST[myc_city]));
	$myc_state   = htmlspecialchars(trim($_POST[myc_state]));
	$myc_zipcode   = htmlspecialchars(trim($_POST[myc_zipcode]));
	$myc_country   = htmlspecialchars(trim($_POST[myc_country]));
	$myc_mobilephone      = htmlspecialchars(trim($_POST[myc_mobilephone]));
	$myc_phone     = htmlspecialchars(trim($_POST[myc_phone]));
	$myc_fax  = htmlspecialchars(trim($_POST[myc_fax]));
	$myc_website   = htmlspecialchars(trim($_POST[myc_website]));
	$myc_industry   = htmlspecialchars(trim($_POST[myc_industry]));
	$myc_speciality1   = htmlspecialchars(trim($_POST[myc_speciality1]));
	$myc_speciality2    = htmlspecialchars(trim($_POST[myc_speciality2]));
	$myc_speciality3       = htmlspecialchars(trim($_POST[myc_speciality3]));
	$myc_notes      = htmlspecialchars(trim($_POST[myc_notes]));

	$sql = "UPDATE	`pa_mycontacts` SET 
		`myc_rcid` = '".$myc_rcid."',
		`myc_firstname` = '".$myc_firstname."',
		`myc_lastname` = '".$myc_lastname."',
		`myc_email` = '".$myc_email."',
		`myc_jobtitle` = '".$myc_jobtitle."',
		`myc_company` = '".$myc_company."',
		`myc_address1` = '".$myc_address1."',
		`myc_address2` = '".$myc_address2."',
		`myc_city` = '".$myc_city."',
		`myc_state` = '".$myc_state."',
		`myc_zipcode` = '".$myc_zipcode."',
		`myc_country` = '".$myc_country."',
		`myc_mobilephone` = '".$myc_mobilephone."',
		`myc_phone` = '".$myc_phone."',
		`myc_fax` = '".$myc_fax."',
		`myc_website` = '".$myc_website."',
		`myc_industry` = '".$myc_industry."',
		`myc_speciality1` = '".$myc_speciality1."',
		`myc_speciality2` = '".$myc_speciality2."',
		`myc_speciality3` = '".$myc_speciality3."',
		`myc_notes` = '".$myc_notes."'
		WHERE myc_id = '".$myc_id."'
	";
	$DB->query($sql);
    redirect("./mycontact.php?action=list&offset=".$offset,"<font color=red>Modified Successfully!</font>");
}
cpfooter();
?>